﻿using System;
using System.Data;
using System.Data.SqlClient;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class DIC_HOLIDAY
    {
        private string m_Description;
        private DateTime m_FromDate;
        private Guid m_HolidayID;
        private string m_HolidayName;
        private DateTime m_ToDate;

        public DIC_HOLIDAY()
        {
            m_HolidayID = Guid.Empty;
            m_HolidayName = "";
            m_Description = "";
            m_FromDate = DateTime.Now;
            m_ToDate = DateTime.Now;
        }

        public DIC_HOLIDAY(Guid HolidayID, string HolidayName, DateTime FromDate, DateTime ToDate, string Description)
        {
            m_HolidayID = HolidayID;
            m_HolidayName = HolidayName;
            m_Description = Description;
            m_FromDate = FromDate;
            m_ToDate = ToDate;
        }

        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        public DateTime FromDate
        {
            get { return m_FromDate; }
            set { m_FromDate = value; }
        }

        public Guid HolidayID
        {
            get { return m_HolidayID; }
            set { m_HolidayID = value; }
        }

        public string HolidayName
        {
            get { return m_HolidayName; }
            set { m_HolidayName = value; }
        }

        public string ProductName
        {
            get { return "Class DIC_HOLIDAY"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        public DateTime ToDate
        {
            get { return m_ToDate; }
            set { m_ToDate = value; }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "HolidayName", "HolidayID");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["HolidayID"] = "All";
            row["HolidayName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "HolidayName", "HolidayID");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["HolidayName"]);
            }
        }

        public DataTable CreateNullDataTable()
        {
            var helper = new SqlHelper();
            var table = new DataTable();
            var column = new DataColumn("HolidayID");
            var column2 = new DataColumn("HolidayName");
            var column3 = new DataColumn("Date");
            var column4 = new DataColumn("Description");
            table.Columns.Add(column);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Clear();
            return table;
        }

        public string Delete()
        {
            string[] myParams = {"@HolidayID"};
            object[] myValues = {m_HolidayID};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Delete", myParams, myValues);
        }

        public string Delete(Guid HolidayID)
        {
            string[] myParams = {"@HolidayID"};
            object[] myValues = {HolidayID};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Delete", myParams, myValues);
        }

        public bool Exist(DateTime Date)
        {
            bool hasRows = false;
            string[] myParams = {"@Date"};
            object[] myValues = {Date};
            var helper = new SqlHelper
            {
                CommandType = CommandType.Text
            };
            SqlDataReader reader =
                helper.ExecuteReader(
                    "select * from DIC_HOLIDAY where dateadd(day,-1,FromDate)<@Date and ToDate>=@Date", myParams,
                    myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public bool Exist(string HolidayID)
        {
            bool hasRows = false;
            string[] myParams = {"@HolidayID"};
            object[] myValues = {HolidayID};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_HOLIDAY_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string HolidayID)
        {
            string str = "";
            string[] myParams = {"@HolidayID"};
            object[] myValues = {HolidayID};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_HOLIDAY_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_HolidayID = reader.GetGuid(reader.GetOrdinal("HolidayID"));
                    m_HolidayName = Convert.ToString(reader["HolidayName"]);
                    m_FromDate = Convert.ToDateTime(reader["FromDate"]);
                    m_ToDate = Convert.ToDateTime(reader["ToDate"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string HolidayID)
        {
            string str = "";
            string[] myParams = {"@HolidayID"};
            object[] myValues = {HolidayID};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "DIC_HOLIDAY_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_HolidayID = reader.GetGuid(reader.GetOrdinal("HolidayID"));
                    m_HolidayName = Convert.ToString(reader["HolidayName"]);
                    m_FromDate = Convert.ToDateTime(reader["FromDate"]);
                    m_ToDate = Convert.ToDateTime(reader["ToDate"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string HolidayID)
        {
            string str = "";
            string[] myParams = {"@HolidayID"};
            object[] myValues = {HolidayID};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "DIC_HOLIDAY_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_HolidayID = reader.GetGuid(reader.GetOrdinal("HolidayID"));
                    m_HolidayName = Convert.ToString(reader["HolidayName"]);
                    m_FromDate = Convert.ToDateTime(reader["FromDate"]);
                    m_ToDate = Convert.ToDateTime(reader["ToDate"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetByMonth(int Month, int Year)
        {
            string[] myParams = {"@Month", "@Year"};
            object[] myValues = {Month, Year};
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_HOLIDAY_GetByMonth", myParams, myValues);
        }

        public DataTable GetByYear(int Year)
        {
            string[] myParams = {"@Year"};
            object[] myValues = {Year};
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_HOLIDAY_GetByYear", myParams, myValues);
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_HOLIDAY_GetList");
        }

        public string Insert()
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {m_HolidayID, m_HolidayName, m_FromDate, m_ToDate, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Insert", myParams, myValues);
        }

        public string Insert(Guid HolidayID, string HolidayName, DateTime FromDate, DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, Guid HolidayID, string HolidayName, DateTime FromDate,
            DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_HOLIDAY_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, Guid HolidayID, string HolidayName, DateTime FromDate,
            DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_HOLIDAY_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("DIC_HOLIDAY", "HolidayID", "TH");
        }

        public string Update()
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {m_HolidayID, m_HolidayName, m_FromDate, m_ToDate, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Update", myParams, myValues);
        }

        public string Update(Guid HolidayID, string HolidayName, DateTime FromDate, DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_HOLIDAY_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, Guid HolidayID, string HolidayName, DateTime FromDate,
            DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_HOLIDAY_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, Guid HolidayID, string HolidayName, DateTime FromDate,
            DateTime ToDate, string Description)
        {
            string[] myParams = {"@HolidayID", "@HolidayName", "@FromDate", "@ToDate", "@Description"};
            object[] myValues = {HolidayID, HolidayName, FromDate, ToDate, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_HOLIDAY_Update", myParams, myValues);
        }
    }
}